﻿(function (w) {
    function importClick(urlArg) {
        var options = {
            url: urlArg.url,
            width: 750,
            allowMaximize: false,
            dialogReturnValueCallback: function (result, returnVal) {
                SP.UI.ModalDialog.RefreshPage(result);
            }
        };

        SP.UI.ModalDialog.showModalDialog(options);
    }

    function exportClick() {
        var listId = SP.ListOperation.Selection.getSelectedList();
        var options = {
            url: SP.Utilities.Utility.getLayoutsPageUrl("UE.SharePoint.ImportExport/ExportPage.aspx?ListId=" + listId),
            width: 550,
            height: 260,
            allowMaximize: false,
            dialogReturnValueCallback: function (result, returnVal) {
                if (result == 4) {
                    notify(returnVal);
                }
            }
        };

        SP.UI.ModalDialog.showModalDialog(options);
    }

    function enabledExportScript() {
        return true;
    }

    function exportSchemaClick() {
        var listId = SP.ListOperation.Selection.getSelectedList();
        var options = {
            url: SP.Utilities.Utility.getLayoutsPageUrl("UE.SharePoint.ImportExport/ExportSchema.aspx?ListId=" + listId),
            width: 550,
            height: 260,
            allowMaximize: false,
            dialogReturnValueCallback: function (result, returnVal) {
                if (result == 4) {
                    notify(returnVal);
                }
            }
        };

        SP.UI.ModalDialog.showModalDialog(options);
    }

    function enabledExportSchemaScript() {
        return true;
    }

    var userIsContrib = null;
    ExecuteOrDelayUntilScriptLoaded(checkUserIsContrib, "sp.js");

    function enabledScript() {
        if (userIsContrib == null) {
            checkUserIsContrib();
            return false;
        }
        else {
            return userIsContrib;
        }
    }

    function checkUserIsContrib() {

        function gotUserIsContrib() {
            // update the variable based on the users permission
            userIsContrib = list.get_effectiveBasePermissions().has(SP.PermissionKind.addListItems);
            //Update the UI
            RefreshCommandUI();
        }

        function failedUserIsContrib() {
        }

        var context = SP.ClientContext.get_current();
        var web = context.get_web();
        var listId;

        try {
            listId = SP.ListOperation.Selection.getSelectedList();
        }
        catch (e) {
        }

        if (!listId) {
            listId = _spPageContextInfo.pageListId;
        }

        if (listId) {
            var list = web.get_lists().getById(listId);
            context.load(list, 'EffectiveBasePermissions');
            context.executeQueryAsync(Function.createDelegate(this, gotUserIsContrib),
                Function.createDelegate(this, failedUserIsContrib));
        }
    }

    if (!w.UE) {
        w.UE = {};
    }
    if (!w.UE.SharePoint) {
        w.UE.SharePoint = {};
    }
    if (!w.UE.SharePoint.ImportExport) {
        w.UE.SharePoint.ImportExport = {};
    }
    if (!w.UE.SharePoint.ImportExport.IE) {
        w.UE.SharePoint.ImportExport.IE = {};
    }

    w.UE.SharePoint.ImportExport.IE.importClick = importClick;
    w.UE.SharePoint.ImportExport.IE.enabledScript = enabledScript;
    w.UE.SharePoint.ImportExport.IE.exportClick = exportClick;
    w.UE.SharePoint.ImportExport.IE.enabledExportScript = enabledExportScript;
    w.UE.SharePoint.ImportExport.IE.exportSchemaClick = exportSchemaClick;
    w.UE.SharePoint.ImportExport.IE.enabledExportSchemaScript = enabledExportSchemaScript; 
})(window)